﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.DTOs;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——采购单
    /// </summary>
    public class InOrderService : CanDoo.Data.Entity.Services.ServiceBase<InOrder>, IInOrderContract
    {
        public IInBuyBackDetailContract InBuyBackDetailContract { get; set; }
        public IInStoreDetailContract InStoreDetailContract { get; set; }
        public InOrderService()
        {
        
        }

        public string GetNewCode()
        {
            var startStr = "CG";
            var dateStr = DateTime.Now.ToString("yyMMdd");
            var codePrefix = startStr + dateStr;

            //生成新单据号
            var newCode = "";
            var entity = Entities.Where(p => p.Code.StartsWith(codePrefix)).OrderByDescending(p => p.Code).FirstOrDefault();
            if (entity == null)
                newCode = codePrefix + "0001";
            else
                newCode = startStr + (int.Parse(entity.Code.Substring(startStr.Length)) + 1);
            return newCode;
        }

        public OperationResult UpdateNum(long entityId)
        {
            var result = OperationResult.Success;
            using (var scope = DbScopeFactory.Create())
            {
                var entity = GetById(entityId);
                bool IsAllGet = true;
                bool IsNoGet = true;

                var thisTotalNum = 0.0;
                var thisTotalMoney = 0.0;

                foreach (var m in entity.InOrderDetails)
                {
                    var InNumSum = InStoreDetailContract.Entities.Where(p=> p.InStore.PassState==PassState.Pass && p.LinkEntityId==m.Id).Select(p => p.Num).DefaultIfEmpty(0.00).Sum();
                    var InMoneySum = InStoreDetailContract.Entities.Where(p => p.InStore.PassState == PassState.Pass  && p.LinkEntityId == m.Id).Select(p => p.Money).DefaultIfEmpty(0.00).Sum();

                    var OutNumSum = InBuyBackDetailContract.Entities.Where(p => p.InBuyBack.PassState == PassState.Pass  && p.LinkEntity.LinkEntityId == m.Id).Select(p => p.Num).DefaultIfEmpty(0.00).Sum();
                    var OutMoneySum = InBuyBackDetailContract.Entities.Where(p => p.InBuyBack.PassState == PassState.Pass && p.LinkEntity.LinkEntityId == m.Id).Select(p => p.Money).DefaultIfEmpty(0.00).Sum();

                    var inStoreNum = InNumSum - OutNumSum;
                    var inStoreMoney = InMoneySum - OutMoneySum;
                    if (inStoreNum < 0)
                        inStoreNum = 0;

                    if (inStoreMoney < 0)
                        inStoreMoney = 0;

                    if (inStoreNum > 0)
                        IsNoGet = false;
                    if (inStoreNum < m.Num)
                        IsAllGet = false;

                    m.AlreadyNum = inStoreNum;
                    m.AlreadyMoney = inStoreMoney;

                    thisTotalNum += inStoreNum;
                    thisTotalMoney += inStoreMoney;
                }

                entity.AlreadyMoney = thisTotalMoney;
                entity.AlreadyNum = thisTotalNum;
                if (IsNoGet)
                    entity.Status = 1;
                if (IsAllGet)
                    entity.Status = 3;
                if (!IsNoGet && !IsAllGet)
                    entity.Status = 2;
                UpdateEntity(entity);
                scope.SaveChanges();
            }
            return result;
        }

        //public OperationResult InToStore(InOrder inOrder, IEnumerable<InStoreDetail> inStoreDetails)
        //{
        //    var result = OperationResult.Success;

        //    bool IsAllGet = true; //是否全部收货
        //    var thisTotalNum = 0.0;
        //    var thisTotalMoney = 0.0;
        //    foreach (var m in inStoreDetails)
        //    {
        //        m.LinkEntity.AlreadyNum = m.LinkEntity.AlreadyNum + m.Num;
        //        m.LinkEntity.AlreadyMoney = m.LinkEntity.AlreadyMoney + m.Money;
        //        thisTotalNum += m.Num;
        //        thisTotalMoney += m.Money;
        //        if (m.LinkEntity.AlreadyNum < m.LinkEntity.Num) //有一个数量不足 就标记为部分收货
        //            IsAllGet = false;
        //    }
        //    inOrder.AlreadyNum = inOrder.AlreadyNum + thisTotalNum;
        //    inOrder.AlreadyMoney = inOrder.AlreadyMoney + thisTotalMoney;
        //    if (IsAllGet)
        //        inOrder.Status = 3;
        //    else
        //        inOrder.Status = 2;

        //    return result;
        //}

        //public OperationResult OutFromStore(InOrder inOrder, IEnumerable<InStoreDetail> inStoreDetails)
        //{
        //    var result = OperationResult.Success;

        //    bool IsAllReturn = true; //是否全部退货
        //    var thisTotalNum = 0.0;
        //    var thisTotalMoney = 0.0;
        //    foreach (var m in inStoreDetails)
        //    {
        //        m.LinkEntity.AlreadyNum = m.LinkEntity.AlreadyNum - m.Num;
        //        m.LinkEntity.AlreadyMoney = m.LinkEntity.AlreadyMoney - m.Money;
        //        thisTotalNum += m.Num;
        //        thisTotalMoney += m.Money;

        //        if (m.LinkEntity.AlreadyNum > 0) //有一个数量不足 就标记为部分收货
        //            IsAllReturn = false;
        //    }
        //    inOrder.AlreadyNum = inOrder.AlreadyNum - thisTotalNum;
        //    inOrder.AlreadyMoney = inOrder.AlreadyMoney - thisTotalMoney;
        //    if (IsAllReturn)
        //        inOrder.Status = 1;
        //    else
        //        inOrder.Status = 2;

        //    return result;
        //}



        public ProductItem EntityChange(InOrderDetail m)
        {
            var ItemEntity = new ProductItem();
            ItemEntity.Id = m.Id;
            ItemEntity.Code = m.Product.Code;
            ItemEntity.SN = m.Product.SN;
            ItemEntity.Specifications = m.Product.Specifications;
            ItemEntity.ProductId = m.Product.Id;
            ItemEntity.Product = m.Product;
            ItemEntity.Name = m.Product.ShortName;
            ItemEntity.Model = m.Product.Model;
            ItemEntity.Num = m.Num;
            ItemEntity.Unit = m.Product.ProductUnit.Name;
            ItemEntity.Price = m.Price;
            ItemEntity.Money = m.Num * m.Price;
            ItemEntity.ChangeNum = m.AlreadyNum;
            ItemEntity.ChangeMoney = m.AlreadyMoney;
            ItemEntity.Coefficient = 1;
            return ItemEntity;
        }
    }
}
